package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.Visit;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingProjectVo;
import com.zzyl.vo.VisitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingLevelVo> listAll() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.listAll();
        return convertToVOList(nursingLevels);
    }

    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.add(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevelVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 使用分页插件分页
        PageHelper.startPage(pageNum, pageSize);

        Page<NursingLevel> page = nursingLevelMapper.getByPage(name, status);

        return PageResponse.of(page, NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);

        return convertToVO(nursingLevel);
    }

    @Override
    public void update(NursingLevelDto nursingLevelDto) {

        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void deleteById(Long id) {

        nursingLevelMapper.deleteById(id);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }

    /**
     * 将NursingLevel转换为NursingLevelVO
     */
    private NursingLevelVo convertToVO(NursingLevel nursingLevel) {
        return BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
    }

    /**
     * 将List<NursingLevel>转换为List<NursingLevelVO>
     */
    private List<NursingLevelVo> convertToVOList(List<NursingLevel> nursingLevels) {
        return nursingLevels.stream().map(this::convertToVO).collect(Collectors.toList());
    }
}
